<html>
<head><meta charset="utf-8"><title>Is ptr::read followed by mem::forget valid? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ptr.3A.3Aread.20followed.20by.20mem.3A.3Aforget.20valid.3F.html">Is ptr::read followed by mem::forget valid?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="172112530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20ptr%3A%3Aread%20followed%20by%20mem%3A%3Aforget%20valid%3F/near/172112530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Konrad Borowski <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ptr.3A.3Aread.20followed.20by.20mem.3A.3Aforget.20valid.3F.html#172112530">(Jul 31 2019 at 09:36)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span><span class="p">;</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">ptr</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">X</span><span class="p">(</span><span class="nb">Box</span><span class="o">&lt;</span><span class="kt">i32</span><span class="o">&gt;</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">X</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">into_inner</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Box</span><span class="o">&lt;</span><span class="kt">i32</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ptr</span>::<span class="n">read</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">.</span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="n">mem</span>::<span class="n">forget</span><span class="p">(</span><span class="bp">self</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="n">a</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">X</span><span class="p">(</span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="mi">3</span><span class="p">)).</span><span class="n">into_inner</span><span class="p">());</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>Miri complains (Miri evaluation error: trying to reborrow for Unique, but parent tag &lt;1843&gt; does not have an appropriate item in the borrow stack) about this, it's used by <a href="https://github.com/abonander/buf_redux/blob/bc614ae2a87442f3edc750108d8f1488037d3a39/src/lib.rs#L631" target="_blank" title="https://github.com/abonander/buf_redux/blob/bc614ae2a87442f3edc750108d8f1488037d3a39/src/lib.rs#L631">https://github.com/abonander/buf_redux/blob/bc614ae2a87442f3edc750108d8f1488037d3a39/src/lib.rs#L631</a> to retrieve certain struct fields without calling destructor. Should it work this way?</p>



<a name="172114679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20ptr%3A%3Aread%20followed%20by%20mem%3A%3Aforget%20valid%3F/near/172114679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ptr.3A.3Aread.20followed.20by.20mem.3A.3Aforget.20valid.3F.html#172114679">(Jul 31 2019 at 10:12)</a>:</h4>
<p><span class="user-mention" data-user-id="198039">@Konrad Borowski</span> ah, I have seen this pattern before. the issue is that <code>mem::forget</code> is just like any other function call, so when you pass <code>self</code> to <code>mem::forget</code>  you are basically saying "this is the only pointer to that memory, make other pointers invalid". At least that's the case with current Stacked Borrows, which is still experimental.</p>



<a name="172114702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20ptr%3A%3Aread%20followed%20by%20mem%3A%3Aforget%20valid%3F/near/172114702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ptr.3A.3Aread.20followed.20by.20mem.3A.3Aforget.20valid.3F.html#172114702">(Jul 31 2019 at 10:13)</a>:</h4>
<p>The recommended way to avoid that is to entirely avoid <code>mem::forget</code> and use <code>ManuallyDrop</code> instead</p>



<a name="172114779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20ptr%3A%3Aread%20followed%20by%20mem%3A%3Aforget%20valid%3F/near/172114779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ptr.3A.3Aread.20followed.20by.20mem.3A.3Aforget.20valid.3F.html#172114779">(Jul 31 2019 at 10:14)</a>:</h4>
<p>so for your code, something like</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ManuallyDrop</span>::<span class="n">new</span><span class="p">(</span><span class="bp">self</span><span class="p">);</span><span class="w"></span>
<span class="n">ptr</span>::<span class="n">read</span><span class="p">(</span><span class="o">&amp;*</span><span class="n">a</span><span class="p">);</span><span class="w"></span>
</pre></div>



<a name="172114803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is%20ptr%3A%3Aread%20followed%20by%20mem%3A%3Aforget%20valid%3F/near/172114803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Is.20ptr.3A.3Aread.20followed.20by.20mem.3A.3Aforget.20valid.3F.html#172114803">(Jul 31 2019 at 10:14)</a>:</h4>
<p>also see <a href="https://github.com/rust-lang/rust/pull/62610" target="_blank" title="https://github.com/rust-lang/rust/pull/62610">https://github.com/rust-lang/rust/pull/62610</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>